package com.kady.service.impl;

import com.kady.Dao.AdminDao;
import com.kady.Dao.OrderDao;
import com.kady.Dao.impl.AdminImpl;
import com.kady.contant.BeanFactoryConstant;
import com.kady.contant.ExceptionConstant;
import com.kady.contant.MessageConstant;
import com.kady.entity.*;
import com.kady.factory.BeanFactory;
import com.kady.service.OrderService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/27
 * \* Time: 13:48
 * \* 千峰JAVA2101班
 * \* Description: 订单业务层
 * \
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = (OrderDao) BeanFactory.getBean(BeanFactoryConstant.ORDER_DAO);

    /**
     * 获取订单列表
     */
    @Override
    public List<Order> getOrderList() throws SQLException, InvocationTargetException, IllegalAccessException {
        List<Map<String,Object>> res = orderDao.selectOrderList();
        List<Order> list = new ArrayList<>();
        AdminDao adminDao = new AdminImpl();
        for(Map<String,Object> item:res){
            Order order = new Order();
            BeanUtils.populate(order,item);
            /**
             * 连表查询到订单详情，并根据userId查询用户是否为VIP用户
             * 根据是否为VIP用户使用对应的价格填入单价，
             */
            byte isVip = 0;
            //如果未找到用户，就用普通价格
            User user = adminDao.selectUserById(order.getUserId());
            if(user!=null && user.getIs_vip()==1){
                isVip = 1;
            }
            Map priceRes = orderDao.countPrice((String) item.get("orderId"));
            if(priceRes.size()>0){
                order.setTotalPrice(
                        (double) (isVip==1?(priceRes.get("vipTotalPrice")==null?0.00:priceRes.get("vipTotalPrice")):(priceRes.get("totalPrice")==null?0.00:priceRes.get("totalPrice")))
                );
            }
            Table table = new Table((int)item.get("tableId"),(String)item.get("tableName"));
            order.setTable(table);
            list.add(order);
        }
        return list;
    }

    /**
     * 订单结账
     */
    @Override
    public Object settleOrder(String orderId) throws SQLException {
        Order order = orderDao.selectById(orderId);
        if(order==null){
            return new ResultVO(false, ExceptionConstant.NOT_FIND_ORDER_INFO);
        }
        byte idOrdered =  order.getIsOrdered();
        order.setIsOrdered((byte) (idOrdered==0?1:0));
        orderDao.updateStatus(order.getIsOrdered(),order.getOrderId());
        return null;
    }

    /**
     * 获取订单详情列表
     */
    @Override
    public List<OrderDeatil> getDetailList(String orderId,String userId) throws SQLException, InvocationTargetException, IllegalAccessException {
        List<Map<String, Object>> res = orderDao.selectDetailList(orderId);
        List<OrderDeatil> list = new ArrayList<>();
        /**
         * 连表查询到订单详情，并根据userId查询用户是否为VIP用户
         * 根据是否为VIP用户使用对应的价格填入单价，
         */
        byte isVip = 0;
        //如果未找到用户，就用普通价格
        if(userId!=null && !userId.isEmpty()){
            AdminDao adminDao = new AdminImpl();
            User user = adminDao.selectUserById(Long.valueOf(userId));
            if(user!=null && user.getIs_vip()==1){
                isVip = 1;
            }
        }
        for(Map<String,Object> item:res){
            OrderDeatil orderDeatil = new OrderDeatil();
            BeanUtils.populate(orderDeatil,item);
            orderDeatil.setDePrice(isVip==1?(double)item.get("vipPrice"):(double)item.get("price"));
            Food food = new Food((int)item.get("foodId"),(String)item.get("foodName"),(double)item.get("price"),(double)item.get("vipPrice"));
            orderDeatil.setFood(food);
            list.add(orderDeatil);
        }
        return list;
    }

    /**
     * 订单详情数量小计
     */
    @Override
    public long countDetailsById(String foodId) throws SQLException {
        long res = orderDao.countDetailsById(foodId);
        return res;
    }
}
